package cn.algorithms_4ed.base.dataStructure;

import java.util.Iterator;

/**
 * Bag
 * <p/>
 * User: erhu
 * Date: 13-6-11
 * Time: 下午4:38
 */
public class Bag<T> implements Iterable<T> {

    private Node<T> first;
    private int size;

    public void add(T item) {
        Node<T> old_node = first;
        first = new Node<T>();
        first.item = item;
        first.next = old_node;
        size++;
    }

    public int size() {
        return size;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {

            private Node<T> current = first;

            @Override
            public boolean hasNext() {
                return current != null;
            }

            @Override
            public T next() {
                T item = current.item;
                current = current.next;
                return item;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

}
